package gis

import "math"

/*
*

	按照经纬度划分格网
*/
type TGisGridV3 struct {
	StartLat, StartLng float64 // 起始经纬度
	GridLat, GridLng   float64 // 格网经纬度大小
}

func MakeGridV3(startb, startl float64, gridlat, gridlng float64) TGisGridV3 {
	return TGisGridV3{StartLat: startb, StartLng: startl, GridLat: gridlat, GridLng: gridlng}
}

func (this *TGisGridV3) CopyFrom(src *TGisGridV3) {
	this.StartLat = src.StartLat
	this.StartLng = src.StartLng
	this.GridLng = src.GridLng
	this.GridLat = src.GridLat
}

/*
*

	获取左下角的格网经纬度

*
*/
func (this *TGisGridV3) GetLeftBottomGrid(b, l float64) (gridb, gridl float64) {
	gridb = math.Floor((b-this.StartLat)/this.GridLat)*this.GridLat + this.StartLat
	gridl = math.Floor((l-this.StartLng)/this.GridLng)*this.GridLng + this.StartLng
	return
}

/*
*

	获取最近的格网经纬度

*
*/
func (this *TGisGridV3) GetNearestGrid(b, l float64) (gridb, gridl float64) {
	gridb = math.Round((b-this.StartLat)/this.GridLat)*this.GridLat + this.StartLat
	gridl = math.Round((l-this.StartLng)/this.GridLng)*this.GridLng + this.StartLng
	return
}

/*
*

	获取最近的4个格网中心点
	f0, f1, t0, t1

*
*/
func (this *TGisGridV3) FindNearest4Grid(b, l float64) (rval [4]BLPoint) {
	rval[0].B = math.Floor((b-this.StartLat)/this.GridLat)*this.GridLat + this.StartLat
	rval[0].L = math.Floor((l-this.StartLng)/this.GridLng)*this.GridLng + this.StartLng

	rval[1].B = rval[0].B
	rval[1].L = rval[0].L + this.GridLng

	rval[2].B = rval[0].B + this.GridLat
	rval[2].L = rval[0].L

	rval[3].B = rval[0].B + this.GridLat
	rval[3].L = rval[0].L + this.GridLng
	return
}
